package com.seuic.hnjiningposapp.hnapps.ui.view.dialog;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BasePopupView;
import com.seuic.hnjiningposapp.R;
import com.seuic.hnjiningposapp.hnapps.bean.AllDealBean;
import com.seuic.hnjiningposapp.hnapps.util.GetMoneyLvUtils;
import com.seuic.hnjiningposapp.hnapps.util.SelTimePickerUtil;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 本机交易查询
 */
public class TransactionqQueryPp extends BasePopupView {
    Unbinder bind;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.sel_op_type)
    TextView selOpType;
    @BindView(R.id.query_btn)
    Button queryBtn;
    @BindView(R.id.start_time)
    TextView startTime;
    @BindView(R.id.end_time)
    TextView endTime;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.printbtn)
    Button printbtn;
    Context mContext;
    private List<AllDealBean> beans;

    TranSQueryCallback mTranSQueryCallback;
    private Adapter adapter;
    queryAllDeal mQueryAllDeal;

    String AllStarttime;
    String AllEndtime;
    String status;

    public TransactionqQueryPp(@NonNull Context context, TranSQueryCallback tranSQueryCallback, queryAllDeal queryAllDeal) {
        super(context);
        this.mContext = context;
        this.mTranSQueryCallback = tranSQueryCallback;
        this.mQueryAllDeal = queryAllDeal;
    }

    @Override
    protected int getPopupLayoutId() {
        return R.layout.statistics_pp;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        bind = ButterKnife.bind(this);
        title.setText("本机交易明细查询");
        startTime.setText(AllStarttime);
        endTime.setText(AllEndtime);
        if (status.equals("")) {
            selOpType.setText("所有交易");
        } else if (status.equals("S")) {
            selOpType.setText("正常交易");
        } else if (status.equals("F")) {
            selOpType.setText("异常交易");
        }


        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        adapter = new Adapter(R.layout.transactionq_queryp_item_layout, null);
        recyclerView.setAdapter(adapter);
        recyclerView.addItemDecoration(new DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL));
        adapter.setNewData(beans);
    }

    public void setBeans(List<AllDealBean> beans) {
        this.beans = beans;
    }

    public void setAllStarttime(String allStarttime) {
        AllStarttime = allStarttime;
    }

    public void setAllEndtime(String allEndtime) {
        AllEndtime = allEndtime;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    @Override
    protected void onDismiss() {
        super.onDismiss();
        //注解解绑
        if (bind != null && bind != Unbinder.EMPTY) {
            bind.unbind();
        }
        this.bind = null;
    }

    @OnClick({R.id.sel_op_type, R.id.query_btn, R.id.start_time, R.id.end_time, R.id.printbtn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.sel_op_type:
                //选择类型
                String types[] = new String[]{"所有交易", "正常交易", "异常交易"};
                new XPopup.Builder(mContext)
                        .asBottomList("", types, (position, text) -> {
                            selOpType.setText(text);
                        }).show();
                break;
            case R.id.query_btn:
                //开始查询

                String status = "";
                if (selOpType.getText().toString().equals("正常交易")) {
                    status = "S";
                } else if (selOpType.getText().toString().equals("异常交易")) {
                    status = "F";
                }
                mQueryAllDeal.queryData(startTime.getText().toString(), endTime.getText().toString(), status);
                break;
            case R.id.start_time:
                //开始时间
                SelTimePickerUtil.loadData(mContext, data ->
                        startTime.setText(data), "yyyyMMdd", true, true, true, false, false, false);
                SelTimePickerUtil.show();
                break;
            case R.id.end_time:
                //结束时间
                SelTimePickerUtil.loadData(mContext, data ->
                        endTime.setText(data), "yyyyMMdd", true, true, true, false, false, false);
                SelTimePickerUtil.show();
                break;
            case R.id.printbtn:
//打印
                mTranSQueryCallback.TranSQuery(beans);
                break;
        }
    }


    private class Adapter extends BaseQuickAdapter<AllDealBean, BaseViewHolder> {
        public Adapter(int layoutResId, List<AllDealBean> data) {
            super(layoutResId, data);
        }

        @SuppressLint("SetTextI18n")
        @Override
        protected void convert(BaseViewHolder baseViewHolder, AllDealBean bean) {
            TextView num_tv, item_name, item_accountnum, item_money, item_type, device_number,
                    item_date, item_time;
            num_tv = baseViewHolder.findView(R.id.num_tv);//序号
            item_name = baseViewHolder.findView(R.id.item_name);//操作名称

            item_accountnum = baseViewHolder.findView(R.id.item_accountnum);//交易账号
            item_money = baseViewHolder.findView(R.id.item_money);//金额
            item_type = baseViewHolder.findView(R.id.item_type);//交易状态
            item_date = baseViewHolder.findView(R.id.item_date);//日期
            item_time = baseViewHolder.findView(R.id.item_time);//时间
            num_tv.setText("序号: " + (baseViewHolder.getPosition() + 1));
            item_name.setText(bean.getTranname());
            item_accountnum.setText(bean.getAcctno());


            if (bean.getTranam().equals("")) {
                item_money.setText("——");
            } else {
                item_money.setText(bean.getTranam());
            }
            if (bean.getStatus().equals("S")) {
                item_type.setText("正常");
            } else {
                item_type.setText("异常");
            }

            item_date.setText(GetMoneyLvUtils.gettimeNohour(bean.getTransDate()));
            item_time.setText(GetMoneyLvUtils.gettimeHour(bean.getTransTime()));


        }
    }

    public interface TranSQueryCallback {
        void TranSQuery(List<AllDealBean> beans);
    }

    public interface queryAllDeal {
        void queryData(String starttime, String endtime, String status);
    }
}
